package com.example.AIstudy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.AIstudy.DTO.ProsetDTO;
import com.example.AIstudy.domain.Exercise;
import com.example.AIstudy.domain.ProSet;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.ExerciseService;
import com.example.AIstudy.service.ProSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/proset")
@Api(tags = "套题管理API")
public class ProSetController {

    @Autowired
    ProSetService proSetService;

    @Autowired
    ExerciseService exerciseService;

    private String singleExe[]={"xuanze","zuowen","yuedu"};
    private String noSingleExe[]={"yuedu","wanxing","xuanci"};


    @PostMapping("")
    @ApiOperation("新增一条套题记录")
//    @PreAuthorize("hasAnyAuthority(#proSet.stuID.toString())")
    public void InsertProSet(@ApiParam("套题信息")@RequestBody ProSet proSet) throws AIstudyException {
        proSetService.insertProSet(proSet);
    }

    @PutMapping("")
    @ApiOperation("修改一条套题记录")
//    @PreAuthorize("hasAnyAuthority(#proSet.stuID.toString())")
    public void updateProSet(@ApiParam("套题信息")@RequestBody ProSet proSet) throws AIstudyException {
        proSetService.updateProset(proSet);
    }

    @DeleteMapping("/{prosetID}/{teacherID}")
    @ApiOperation("删除一条套题记录")
//    @PreAuthorize("hasAnyAuthority(#stuID.toString())")
    public void deleteProSet(@ApiParam("套题ID")@PathVariable int prosetID,
                             @ApiParam("做套题的学生ID")@PathVariable Integer stuID) throws AIstudyException {
        proSetService.deleteProset(prosetID);
    }

    //根据条件查询套题信息，条件可以为空
    @ApiOperation("根据条件查询套题信息")
    @GetMapping("")
    public IPage<ProSet> findProset(@ApiParam("套题名称")@RequestParam(value = "proSetName",required = false) String proSetName,
                                @ApiParam("套题描述")@RequestParam(value = "proSetDesc",required = false) String proSetDesc,
                                @ApiParam("学生ID")@RequestParam(value = "teacherID",required = false) Integer stuID,
                                @ApiParam("起始时间")@RequestParam(value = "minTime",required = false) Timestamp minTime,
                                @ApiParam("结束时间")@RequestParam(value = "maxTime",required = false) Timestamp maxTime,
                                  @ApiParam("排序字段")@RequestParam(value = "orderField",required = false) String orderField,
                                  @ApiParam("排序方式")@RequestParam(value = "orderType",required = false) String orderType,
                                @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize){
        Map<String,Object> condition=new HashMap<>();
        if(proSetName!=null) {
            condition.put("proSetName",proSetName);
        }
        if(proSetDesc!=null) {
            condition.put("proSetDesc",proSetDesc);
        }
        if(stuID!=null) {
            condition.put("stuID",stuID);
        }
        if(minTime!=null) {
            condition.put("minTime",minTime);
        }
        if(maxTime!=null) {
            condition.put("maxTime",maxTime);
        }
        if(orderField!=null) {
            condition.put("orderField",orderField);
            if(orderType!=null) {
                condition.put("orderType",orderType);
            }
            else condition.put("orderType","ASC");
        }
        return proSetService.findProset(condition, pageNum,pageSize);
    }

    @ApiOperation("查询与套题相关的所有试题")
    @GetMapping("/selectSetDetail")
    public ProsetDTO findProsetDetailByID(@ApiParam("套题ID")@RequestParam(value = "prosetID",required = false) Integer prosetID){

        ProsetDTO prosetDTO=new ProsetDTO();
        prosetDTO.setTestProps(proSetService.findProsetByID(prosetID));
        Map<String,List<Exercise>>question=new HashMap<>();
        //确定套题的选择题情况
        question.put("xuanze", proSetService.findProsetSingleExe(prosetID,"xuanze"));
        System.out.println(proSetService.findProsetSingleExe(prosetID,"xuanze"));
        prosetDTO.setXuanzeNum(question.get("xuanze").size());
        if(!question.get("xuanze").isEmpty()){
            prosetDTO.setXuanzeScore(question.get("xuanze").get(0).getScore());
        }
        //确定套题的翻译题情况
        question.put("fanyi", proSetService.findProsetSingleExe(prosetID,"fanyi"));
        prosetDTO.setFanyiNum(question.get("fanyi").size());
        if(!question.get("fanyi").isEmpty()){
            prosetDTO.setFanyiScore(question.get("fanyi").get(0).getScore());
        }
        //确定套题的作文情况
        question.put("zuowen", proSetService.findProsetSingleExe(prosetID,"zuowen"));
        prosetDTO.setZuowenNum(question.get("zuowen").size());
        if(!question.get("zuowen").isEmpty()){
            prosetDTO.setZuoWenScore(question.get("zuowen").get(0).getScore());
        }
        //确定套题的阅读情况
        List<Exercise>tmpList=proSetService.findProsetNoSingleExe(prosetID,"yuedu");
        int size=tmpList.size();
        prosetDTO.setYueduArticleNum(size);
        int yueduNum=0;
        for(int i=0;i<size;i++){
            Exercise exercise=tmpList.get(i);
            //阅读题自增
//            yueduNum++;
            //确定每道阅读题的分数
            prosetDTO.setYueduScore(exercise.getScore());
//            String[] strids =exercise.getSonProID().split(",");
//            int tmpsize=strids.length;
            //增加子题的数量
//            yueduNum+=tmpsize;
//            List<Integer>ids=new ArrayList<>();
//            //获取每道阅读题的子题
//            for(int j=0;j<tmpsize;j++){
//                ids.add(Integer.valueOf(strids[j]));
//            }
//            exercise.setSonExercise(exerciseService.findSonExercise(ids));
            tmpList.set(i, exerciseService.fengzhuangYuedu(exercise));
            yueduNum+=exercise.getSonExercise().size();
//            exeList.add(exercise);
        }
        prosetDTO.setYueduNum(yueduNum);
        question.put("yuedu", tmpList);

        //确定完形填空的阅读情况
//        tmpList=proSetService.findProsetNoSingleExe(prosetID,"wanxing");
//        size=tmpList.size();
//        prosetDTO.setWanxingArticleNum(size);
//        yueduNum=0;
//        for(int i=0;i<size;i++){
//            Exercise exercise=tmpList.get(i);
//            //阅读题自增
//            yueduNum++;
//            //确定每道阅读题的分数
//            prosetDTO.setWanxingScore(exercise.getScore());
//            String[] strids =exercise.getSonProID().split(",");
//            int tmpsize=strids.length;
//            //增加子题的数量
//            yueduNum+=tmpsize;
//            List<Integer>ids=new ArrayList<>();
//            //获取每道阅读题的子题
//            for(int j=0;j<tmpsize;j++){
//                ids.add(Integer.valueOf(strids[j]));
//            }
//            exercise.setSonExercise(exerciseService.findSonExercise(ids));
//        }
//        question.put("wanxing", tmpList);
//        prosetDTO.setWanxingNum(yueduNum);

        //确定选词填空的阅读情况
//        tmpList=proSetService.findProsetNoSingleExe(prosetID,"xuanci");
//        size=tmpList.size();
//        prosetDTO.setXuanciArticleNum(size);
//        yueduNum=0;
//        for(int i=0;i<size;i++){
//            Exercise exercise=tmpList.get(i);
//            //阅读题自增
//            yueduNum++;
//            //确定每道阅读题的分数
//            prosetDTO.setXuanzeScore(exercise.getScore());
//            String[] strids =exercise.getSonProID().split(",");
//            int tmpsize=strids.length;
//            //增加子题的数量
//            yueduNum+=tmpsize;
//            List<Integer>ids=new ArrayList<>();
//            //获取每道阅读题的子题
//            for(int j=0;j<tmpsize;j++){
//                ids.add(Integer.valueOf(strids[j]));
//            }
//            exercise.setSonExercise(exerciseService.findSonExercise(ids));
//        }
//        prosetDTO.setXuanciNum(yueduNum);
//        question.put("xuanci", tmpList);
        prosetDTO.setQuestion(question);
        return prosetDTO;
    }

//    @PostMapping("/like")
//    @ApiOperation("点赞一个套题")
//    @PreAuthorize("hasAnyAuthority(#liked.userID.toString())")
//    public void likeProSet(@ApiParam("点赞信息")@RequestBody Liked liked) throws AIstudyException {
//        proSetService.insertLiked(liked);
//    }

    @ApiOperation("传递一个json，封装套题的详细信息，完成添加")
    @PostMapping("/insertDetail")
    public void insertProsetDetail(@ApiParam("套题的细节信息")@RequestBody ProsetDTO prosetDTO) throws AIstudyException {
            proSetService.insertProsetDetail(prosetDTO);
    }

    @ApiOperation("传递一个json，封装套题的详细信息，完成更新操作")
    @PutMapping("/updateDetail")
    public void updateProsetDetail(@ApiParam("套题的细节信息")@RequestBody ProsetDTO prosetDTO) throws AIstudyException {
        proSetService.updateProsetDetail(prosetDTO);
    }
}
